package com.xiaosu.red_travel.controller;

import com.xiaosu.red_travel.bean.ResultInfo;
import com.xiaosu.red_travel.mapper.Comment;
import com.xiaosu.red_travel.mapper.Fabulous;
import com.xiaosu.red_travel.mapper.User;
import com.xiaosu.red_travel.service.CommentService;
import com.xiaosu.red_travel.service.FabulousService;
import com.xiaosu.red_travel.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RouteService routeService;

    @Autowired
    private FabulousService fabulousService;


    /**
     * 用户评论
     * @param comment 评论对象
     * @return 结果对象
     */
    @RequestMapping("/addComment")
    public ResultInfo addComment(@Validated Comment comment, HttpSession session){

        /*User loginUser = (User) session.getAttribute("login_user"+comment.getUid());
        if(loginUser==null){
            return ResultInfo.no("您还未登录");
        }*/
        boolean save = commentService.save(comment);
        // 添加评论数量
        if(save){
            routeService.addCommentCount(comment.getRid());
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("评论失败");
        }
    }

    /**
     * 根据景点id查询景点评论
     * @param rid 景点id
     * @return 结果对象
     */
    @RequestMapping("/getCommentByRid")
    public ResultInfo getCommentByRid(Integer rid){
        List<Comment> commentByRid = routeService.getCommentByRid(rid);
        if(commentByRid==null){
            return ResultInfo.no("未找到此线路");
        }
        if(commentByRid.size()==0){
            return ResultInfo.ok("此线路暂无评论");
        }else {
            return ResultInfo.ok(commentByRid);
        }
    }

    /**
     * 点赞
     * @param fabulous 点赞赞对象
     * @return 结果对象
     */
    @RequestMapping("/addFabulous")
    public ResultInfo addFabulous(Fabulous fabulous,HttpSession session){
        User loginUser = (User) session.getAttribute("login_user"+fabulous.getUid());
        if(loginUser==null){
            return ResultInfo.no("您还未登录");
        }
        fabulous.setFtime(new Date());
        boolean save = fabulousService.save(fabulous);
        if(save){
            return ResultInfo.ok("此线路暂无评论");
        }else {
            return ResultInfo.ok();
        }
    }

}
